package com.shunhe.storage.ui.repo;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.support.view.LinearItemDecoration;
import com.shunhe.storage.R;
import com.shunhe.storage.dao.model.Goods;
import com.shunhe.storage.ui.adapter.SwipeGoodsListAdapter;
import com.shunhe.storage.ui.goods.ChGoodsActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class InRepoOrderActivity extends BaseActivity {
    private RecyclerView rcv_goods_list;// 选择的货品列表
    private TextView tv_ch_goods_0;// 选择/添加货品

    private SwipeGoodsListAdapter adapter;
    private List<Goods> goodsList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_in_repo_order;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_goods_list = findViewById(R.id.rcv_goods_list);
        tv_ch_goods_0 = findViewById(R.id.tv_ch_goods_0);

        setHeadTitle("入库下单");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new SwipeGoodsListAdapter(this);
        LinearItemDecoration divider = new LinearItemDecoration.Builder(this)
                .setSpan(R.dimen.deco_10_dp)
//                .setPadding(R.dimen.line_width)
//                .setLeftPadding(R.dimen.deco_10_dp)
//                .setRightPadding(R.dimen.deco_10_dp)
                .setColorResource(R.color.divider_list)
//                .setShowLastLine(true)
                .build();
        rcv_goods_list.addItemDecoration(divider);
        rcv_goods_list.setLayoutManager(manager);
        rcv_goods_list.setAdapter(adapter);
        for (int i = 0; i < 5; i++) {
            goodsList.add(new Goods());
        }
        adapter.setDataList(goodsList);

        setListeners();

        Arad.bus.register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(EventModel ev) {
        if (ev.getEventCode() == 20120521) {// 添加商品
            goodsList.add(new Goods());
            adapter.setDataList(goodsList);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    private void setListeners() {
        setOnClickListener(tv_ch_goods_0);
        adapter.setOnSwipeListener(new SwipeGoodsListAdapter.OnSwipeListener() {
            @Override
            public void onDel(Object obj, int index) {
                Goods goods = (Goods) obj;
                ToastUtils.showShort("删除  index = " + index);
                goodsList.remove(index);
                adapter.setDataList(goodsList);
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_ch_goods_0) {// 选择货品
            startActivity(ChGoodsActivity.class);
        }
    }

//    private class GoodsListAdapter extends ListBaseAdapter<Goods> {
//
//        public GoodsListAdapter(Context context) {
//            super(context);
//        }
//
//        @Override
//        public int getLayoutId() {
//            return R.layout.item_goods_list;
//        }
//
//        @Override
//        public void onBindItemHolder(SuperViewHolder holder, int position) {
//
//        }
//    }
}